<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../common.inc" %>    
<%@ page language="java" import="com.feeyin.radar.crm.model.CodeStore" %>
<%@ page language="java" import="com.feeyin.radar.sys.model.Users" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link href="./css/style1.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/calendar.js"></script>
<script type="text/javascript" src="./js/form.js"></script>
<script type="text/javascript" src="./js/jquery.js"></script>
<script type="text/javascript" src="./js/business/contractcase.js"></script>
<script type="text/javascript" language="javascript">

	function checkForm(){
		return true;
	}

	function back(){
		window.location.href = "./contract.do";
		window.location.target = "main";
	}
	
	function customerDialog(){
		var url = "./contract.do?action=selectCustomer";
		var dlgParam = "dialogHeight:360px;dialogWidth:430px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取客户id和客户名称
			var customerId = "";
			var customerName = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				customerId = tStr[0];
				customerName = tStr[1];
			}
			document.getElementById("customerId").value = customerId;										
			document.getElementById("customerName").value = customerName;
			document.getElementById("contractName").value = customerName + "销售合同";
		}
	}
	
	function productDialog(){
		var url = "selectProduct.html";
		var dlgParam = "dialogHeight:400px;dialogWidth:500px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		window.showModalDialog(url,"",dlgParam);
	}
	
	function save(){
		if(checkForm()){
			document.addContractForm.action = "./contract.do?action=addContract";
			document.addContractForm.target = "main";
			document.addContractForm.submit();
			
			//document.getElementById("saveBtn").href = "#";
		}
	}
	
	function getCostsMain(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
	}
	//计算成本
	function getCosts(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后的金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0 && 
			costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("costs")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(prossProfits[rowNum].value);
			}
		}
	}
	
	function getProssProfitMain(obj,rowNum){
		onlyNumFloat(obj);
		getProssProfit(rowNum);
	}
	//计算毛利
	function getProssProfit(rowNum){
		rowNum = Number(rowNum) - 1;
		var costValues = document.getElementsByName("costValue");//折扣后的金额
		//var discounts = document.getElementsByName("discount");//折扣率
		var costss = document.getElementsByName("costs");//成本
		var prossProfits = document.getElementsByName("prossProfit");//毛利
		if(costValues.length > 0  
			&& costss.length > 0 && prossProfits.length > 0){
			if(costValues[rowNum].value != ""){
				document.getElementsByName("prossProfit")[rowNum].value = 
					Number(costValues[rowNum].value) - Number(costss[rowNum].value);
			}
		}
	}
	
	function getCostsAndProssProfitMain(rowNum){
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	function getCostsAndProssProfit(obj,rowNum){
		onlyNumFloat(obj);
		getCosts(rowNum);
		getProssProfit(rowNum);
	}
	
	function personDialog(){
		var url = "./contract.do?action=selectSalePerson";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var rt = window.showModalDialog(url,"",dlgParam);
		if(rt != undefined){
			//根据从子窗口得到的值，从中提取销售代表id和销售代表名称
			var salePersonId = "";
			var salePerson = "";
			var tStr = rt.split(",");
			if(tStr[0] != 0 && tStr[1] != 0){
				salePersonId = tStr[0];
				salePerson = tStr[1];
			}
			document.getElementById("salePersonId").value = salePersonId;										
			document.getElementById("salePerson").value = salePerson;
		}
	}
	
	function selectCaseType(tableId){
		var url = "./contract.do?action=selectCaseType";
		var dlgParam = "dialogHeight:350px;dialogWidth:400px;dialogTop:300px;dialogLeft:400px;status:no;help:no;scrollbars:yes;";
		var vtypeId = window.showModalDialog(url,"",dlgParam);
		var vcaseCode ;
		var vfirstProcess;
		var vprocessId;
		var vprocessGroupId;
		if(vtypeId != undefined){
			$.getJSON("./contract.do?action=selectCaseTypeDetail",
					  { 
						   typeId:vtypeId //发送给数据库的数据
					  },
					 function(data) {
				  $.each(data, function(key, val) {
				        if(key=='caseCode'){
				        	vcaseCode = val;
				        }	
				        if(key=='firstProcess'){
				        	vfirstProcess =val;
				        }
				        if(key=='processId'){
				        	vprocessId = val;
				        }
                        if(key=='processGroupId'){
                        	vprocessGroupId = val;
				        }
				        if(vcaseCode!=null&&vfirstProcess!=null&&vprocessId!=null&&vprocessGroupId!=null){
				        	var insertTdHtml = prepareInsertHtml(tableId,newTrNum,vcaseCode,'','',vtypeId,vfirstProcess,vprocessId,vprocessGroupId,1);//获得新增行各个TD的HTML
							var mark = vcaseCode+'_'+vtypeId;
				        	addRow(tableId,insertTdHtml,mark);
				        }
				    	
				  });
				});
		
		}
	}
	

	//追加行start
	function addRow(tableId,insertTdHtml,mark){//增加tab的一行	
		try{
			var objT =  document.getElementById(tableId);//通过ID 得到对象
			if(objT==null){	alert("没有可操作的表！");return;	}
			if(objT.rows.length<1){ alert("没有表格标题行"); return;}
			
			var newTr = objT.insertRow(-1);//插入一行
			newTr.id=mark;
			newTr.className = "table_style";	//为新行赋样式
			var colsLength = objT.rows(0).cells.length;  //表中列总数 
			var newTd ;
			var hiddenObj;		
		
			var i=0;
			for(i=0;i<colsLength;i++){
				newTd = newTr.insertCell();
				newTd.innerHTML = insertTdHtml[i];
				newTd.align="left";				
			}
			newTrNum++;//表格总行数加1
		}catch(Exception){}
	}
	
	//+tableId+","+num+","
	//增加行所需代码
	var newTrNum = 0;//表格最大行号，只增不减
	
	//追加行end
	
	function initPage(){
		newTrNum = table1.rows.length;//表格最大行号，只增不减
	
		if("<c:out value='${result}'/>" == 1){
			alert("<fmt:message key='common.message.addSuccess'/>");
		}else if("<c:out value='${failResult}'/>" == 3){
			alert("保存失败：上传文件大小不能超过10M!");
		}else if("<c:out value='${repeatResult}'/>" == 1){
			alert("保存失败：合同编码已存!");
		}
		
		document.getElementById("contractCode").value = "HT" + "<%=((Users)request.getAttribute("user")).getUserCode()%>" + "<%=((CodeStore)request.getAttribute("codeStore")).getNextId()%>";		

		if("<c:out value='${contract.contractDesc}'/>" == ""){
			document.getElementById("contractDesc").value = "\n预计初稿完成时间(由代理人输入)：";
			}
		//document.getElementById("contractDesc").value = "\n\n\n预计初稿完成时间(由代理人输入)：";
		//document.getElementById("saveBtn").href = "javascript:save();";
	}
	
	function isDummyCheck(obj)
	{
		if(obj.checked == true)
			document.getElementById("contractCode").value = document.getElementById("contractCode").value + "V";
		else
		{
			var contractCode = 	document.getElementById("contractCode").value;
			if(contractCode.indexOf("V") > 0)
				document.getElementById("contractCode").value = contractCode.substr(0, contractCode.indexOf("V"));
		}

	}
	
	
	function deleteContractCase()
	{
		var caseCodeStr = "";
		var delAry = doucment.getElementsByName("delConCase");
		for(var i=0; i<delAry.length; i++)
		{
			if(delAry[i].checked)
			{
				var conCaseId = document.getElementById("delConCase_"+i).value;
				if(caseCodeStr != "")
					caseCodeStr = caseCodeStr + "/" + conCaseId;
				else
					caseCodeStr = conCaseId;
			}
		}
		
		if(confirm("Are you sure to delete these Contract Case?"))
		{
			document.getElementById("delContractCaseStr").value = caseCodeStr;
			
			document.addContractForm.action = "./contract.do?action=deleteContractCase";
			document.addContractForm.target = "main";
			document.addContractForm.submit();			
		}
	}
</script>
</head>
<body onload="initPage()">
<form name="addContractForm" action="" method="post" enctype="multipart/form-data">
	<table class="table_frame">
		<tr>
			<td>
				<table class="table_title1">
					<tr>
						<td class="title_lab">申请合同</td>
					</tr>
					<tr>
						<td align="center" valign="bottom">
							<input class="btn_submit" type="button" value="提交" onclick="save()" hidefocus="true"/>
                            <input class="btn_back" type="button" value="取消并返回" onclick="back()" hidefocus="true"/>
						</td>
					</tr>
					<tr><td height="2"></td></tr>
				</table>
			</td>
		</tr>
		<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr><td colspan="4" class="title_top">合同基本信息</td></tr>
								<tr>
									<td class="title">合同编号：</td>
									<td class="">
										<input id="contractCode" name="contractCode" class="input_long" type="text" value="${contract.contractCode}" readonly/>
										<input id="isDummy" name="isDummy" type="checkbox" value="0" onclick="isDummyCheck(this)"/>
									</td>
									<td class="title">客户名称：</td>
									<td class="">
										<input id="customerName" name="customerName" class="input_long" type="text" value="${contract.customerName}" readonly="readonly"/>&nbsp;
										<input class="btn_select" type="button" value="" onclick="customerDialog()" hidefocus="true"/>
										<input id="customerId" name="customerId" type="hidden" value="${contract.customerId}"/>
									</td>
								</tr>
								<tr>
									<td class="title">合同名称：</td>
									<td class=""><input id="contractName" name="contractName" class="input_long" type="text" value="${contract.contractName}"/></td>
									<td class="title">签约时间：</td>
									<td class="">
										<input id="contractDate" name="contractDate" class="input_long" type="text" 
											onclick="calendar(this,'yyyy-MM-dd');" readonly="readonly" value="${contract.contractDate}"/>
									</td>
								</tr>
								<tr>
									<td class="title">销售代表：</td>
									<td class="">
										<c:choose>
											<c:when test="${isViewAllContract == 1}">
											<input id="salePerson" name="salePerson" class="input_long" type="text" value="${contract.salePerson}" readonly="readonly"/>
											</c:when>
											<c:otherwise>${userName}</c:otherwise>
										</c:choose>
										<input id="salePersonId" name="salePersonId" type="hidden" value="${contract.salePersonId}"/>
										<c:if test="${isViewAllContract == 1}">
										<input class="btn_select" type="button" value="" onclick="personDialog()" hidefocus="true"/>
										</c:if>
									</td>
									<td class="title">制单日期：</td>
									<td class="">
										<fmt:formatDate value="<%=new java.util.Date()%>" pattern="yyyy-MM-dd"/>
										<input id="createDate" name="createDate" type="hidden" 
											value="<fmt:formatDate value='${contract.createDate}' pattern='yyyy-MM-dd'/>"/>
										<input id="applyPersonId" name="applyPersonId" type="hidden" value="${userId}"/>
									</td>
								</tr>
								<tr>
									<td class="title"><font style="font-size: 10px;">(附件 &lt; 10M)</font>&nbsp;&nbsp;上传附件：</td>
									<td colspan="3">
										<input id="contents" name="contents" type="file" style="width: 96%"/>
									</td>
								</tr>
								<tr>
									<td class="title">备注：</td>
									<td class="" colspan="3">
										<textarea id="contractDesc" name="contractDesc" class="textarea_remark">${contract.contractDesc}</textarea>
									</td>
								</tr>
							</table>
						</td>
					</tr>
				</table>
			</td>
		</tr>
<tr>
			<td>
				<table class="table_title2">
					<tr>
						<td>
							<table class="table_add">
								<tr>
									<td class="title_top" width="60">合同标的</td>
									<td class="title_top">
										<input class="btn_append" type="button" value="新增案件" onclick="selectCaseType('table1')" hidefocus="true"/>
									</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td>
						<table id="table1" class="table_style">
							<tr>
							    <td class="title">&nbsp;</td>
								<td class="title">案件名称</td>
								<td class="title">&nbsp;</td>
								<td class="title" width="8%">当前进度</td>
								<td class="title" width="12%">总金额</td>
								<td class="title" width="12%">官费</td>
								<td class="title" width="12%">业务费用</td>
								<td class="title" width="12%">代理费用</td>
							</tr>
							<% int i = 1; %>
							<c:forEach var="contractDetail" items="${contractDetails}">
							<tr>
							    <td><a href="">删除</a></td>
								<td>
									<input id="contractCaseName" name="contractCaseName" class="input_percent" type="text" value="${contractDetail.contractCaseName}" readonly/>
									<input id="caseCode" name="caseCode" type="hidden" value="${contractDetail.caseCode}"/>
									<input id="applicationCode" name="applicationCode" type="hidden" value="${contractDetail.applicationCode}"/>
									<input id="description" name="description" type="hidden" value="${contractDetail.description}"/>
									<input id="suffixCaseName" name="suffixCaseName" class="input_percent" type="text" value="${contractDetail.suffixCaseName}"/>
								</td>
								<td><input class='btn_select' type='button' onclick='addContractCase(this)' hidefocus='true'/></td>
								<td><input id="processName" name="processName" class="input_percent" type="text" value="${contractDetail.processName}"/></td>
								
								<td>
									<input id="costValue" name="costValue" class="input_percent" type="text" value="${contractDetail.costValue}"/>
								</td>
								<td>
									<input id="officialCost" name="officialCost" class="input_percent" type="text" value="${contractDetail.officialCost}"/>
								</td>
								<td>
									<input id="operationCost" name="operationCost" class="input_percent" type="text" value="${contractDetail.operationCost}"/>
								</td>
								<td>
									<input id="proxyCost" name="proxyCost" class="input_percent" type="text" value="${contractDetail.proxyCost}"/>
									<input id="applicationDate" name="applicationDate" type="hidden" value="${contractDetail.applicationDate}"/>
								</td>
								 
								<%i = i + 1; %>
							</tr>
							</c:forEach>
						</table>
						</td>
					<tr>
				</table>
			</td>
		</tr>
	</table>
</form>
</body>
</html>